import { ref } from "vue";
import { service, getPendingRequest } from "@/utils/";

export default function useFetch(config) {
  const data = ref(null);
  const loading = ref(false);
  const error = ref(null);
  const cancel = getPendingRequest(config);
  service(config)
    .then(
      (value) => {
        data.value = value;
      },
      (err) => {
        error.value = err;
      }
    )
    .finally(() => {
      loading.value = false;
    });

  return { data, loading, error, cancel };
}
